%%%-----------------------------------
%%% @Module  : pt_18
%%% @Author  : xws
%%% @Created : 2016.9.7
%%% @Description: 
%%%-----------------------------------
-module(pt_18).
-export([read/2, write/2]).
-include("common.hrl").
-include("proto_18_trade.hrl").

%%
%%客户端 -> 服务端 ----------------------------
%%

%% 18000 c2s查找商行物品
read(18000, Bin) ->
	{ClientData, _} = proto_read:unpack(c2s_find_trade_goods, Bin),	
	#c2s_find_trade_goods{
        condition_list = ConditionList
    } = ClientData,
    ConditionList1 = [{GId, Qty} || #proto_find_condition{goods_id=GId, goods_quality=Qty} <- ConditionList],
    {ok, [ConditionList1]};

%% 18001 c2s获取玩家商行数据
read(18001, Bin) ->
	{ClientData, _} = proto_read:unpack(c2s_get_player_trade_data, Bin),	
	#c2s_get_player_trade_data{player_id = PlayerId} = ClientData,
    {ok, [PlayerId]};

%% 18002 c2s上架物品到商行
read(18002, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_sell_trade_goods, Bin),  
    #c2s_sell_trade_goods{
        goods_uid = GoodsUId,
        goods_num = GoodsNum,
        goods_price = Price,
        msg = Msg
    } = ClientData,
    {ok, [GoodsUId, GoodsNum, Price, Msg]};

%% 18003 c2s下架物品
read(18003, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_unsell_trade_goods, Bin),  
    #c2s_unsell_trade_goods{trade_uid = TradeUId} = ClientData,
    {ok, [TradeUId]};

%% 18004 c2s购买物品
read(18004, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_buy_trade_goods, Bin),  
    #c2s_buy_trade_goods{trade_uid = TradeUId} = ClientData,
    {ok, [TradeUId]};

%% 18006 c2s回收过期商行物品
read(18006, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_recover_trade_goods, Bin),  
    #c2s_recover_trade_goods{trade_uid = TradeUId} = ClientData,
    {ok, [TradeUId]};

%% 18007 c2s领取已出售的物品
read(18007, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_pickup_traded_goods, Bin),  
    #c2s_pickup_traded_goods{trade_uid = TradeUId} = ClientData,
    {ok, [TradeUId]};

%% 18008 c2s赠送玩家物品
read(18008, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_give_player_goods, Bin),  
    #c2s_give_player_goods{goods_uid=GUId, num=Num, target_special_id=TSId} = ClientData,
    {ok, [GUId, Num, TSId]};


read(_Cmd, _R) ->
    ?ERROR_MSG("~s pt_14 reading cmd_[~p], Data:~p~n",[misc:time_format(game_timer:now()), _Cmd, _R]),
    {error, no_match}.

%%
%%服务端 -> 客户端 ------------------------------------
%%
%% 18000 s2c 查找商行物品
write(18000, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_find_trade_goods, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(18000, ServerData)};

%% 18001 s2c 获取玩家商行数据
write(18001, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_get_player_trade_data, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(18001, ServerData)};

%% 18002 s2c 上架物品到商行
write(18002, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_sell_trade_goods, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(18002, ServerData)};

%% 18003 s2c 下架物品
write(18003, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_unsell_trade_goods, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(18003, ServerData)};

%% 18004 s2c 购买物品
write(18004, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_buy_trade_goods, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(18004, ServerData)};

%% 18005 s2c 推送玩家物品被购买
write(18005, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_notify_trade_goods_sold, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(18005, ServerData)};

%% 18006 s2c 回收过期商行物品
write(18006, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_recover_trade_goods, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(18006, ServerData)};

%% 18007 s2c 领取已出售的物品
write(18007, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_pickup_traded_goods, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(18007, ServerData)};

%% 18008 s2c 赠送玩家物品
write(18008, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_give_player_goods, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(18008, ServerData)};

write(Cmd, _R) ->
    ?ERROR_MSG("~s pt_14 writting cmd_[~p], Data:~p~n",[misc:time_format(game_timer:now()), Cmd, _R]),
    {ok, pt:pack(0, <<>>)}.
